<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class CartController extends Controller{
	private $goodsModel;
	private $gattrModel;
	//构造函数
	public function __init()
	{
		$this->goodsModel = new \Common\Model\Goods;
		$this->gattrModel = new \Common\Model\Gattr;
	}
	public function index(){
//		获取购物车数据
		$allCartData = Cart::getGoods();
		if(!$allCartData){
			$allCartData = array();
		}
		View::with('allCartData',$allCartData);
		View::make();
	}
	
	
	
	
    //动作
    public function add(){
//  	获得商品id
		$gid = Q('post.gid',0,'intval');
//		获得商品信息
		$goodsData = $this->goodsModel->where("gid = {$gid}")->find();
		$gaid = Q('post.gaid');
		$color = $this->gattrModel->where("gaid = {$gaid[1]}")->pluck('gavalue');
		$size = $this->gattrModel->where("gaid = {$gaid[0]}")->pluck('gavalue');
		$cartData = array(
			'id' 		=> $gid,
			'name' 		=> $goodsData['gname'],
			'num' 		=> Q('post.num',0,'intval'),
			'price' 	=> $goodsData['sprice'],
			'options' 	=> array(
			'color_value'=>$gaid[1],
			'color' 	=> $color,
			'size_value'=> $gaid[0],
			'size' 		=> $size,
			'listpic'	=>$goodsData['listpic'],
			),
			
		);
		Cart::add($cartData);
    }
	public function del(){
//		获得session id
		$id = Q('post.id');
		$data=array( 
		    'sid'=>$id,// 唯一 sid，添加购物车时自动生成 
		    'num'=>0
		); 
		Cart::update($data); 
	}
	
//	清空购物车
	public function delAll(){
		unset($_SESSION['cart']);
		go('index');
	}
	
	public function upDate(){
		$data=array( 
		    'sid'=>Q('post.id'),// 唯一 sid，添加购物车时自动生成 
		    'num'=>Q('post.num',0,'intval')
		); 
		Cart::update($data); 
		echo json_encode(array('status'=>true));
	}
	
//	ajax请求
	public function getCart(){
		$allCartData = Cart::getGoods();
		if(!$allCartData){
			$allCartData = array();
		}
		echo json_encode($allCartData);
	}
	
	
	
	
	
}
